package main.iwith;

import main.util.JsonUtil;
import main.util.WebConn;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class OperatorTest {
	
	public static Sheet sheet;

	public static void main(String[] args) throws IOException {
		
		Workbook wb=null;
		FileOutputStream fos=null;
		try {
			wb = new HSSFWorkbook();
			sheet = wb.createSheet();
			
			//开始生成测试表格
			process();
			
			fos=new FileOutputStream("G:/测试语句.xls");
			wb.write(fos);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(wb!=null){
				wb.close();
			}
			
			if(fos!=null){
				fos.close();
			}
		}
	}

	//测试测试语句，从word里面复制出来
	public static void process(){
		FileReader file;
		try {
			file = new FileReader("G:/测试语句意图是否正确.txt");
			BufferedReader br=new BufferedReader(file);

	        
	        Map<String,Object> map=new HashMap<>();
	        String line ;
	        int rowNum=0;
	        String intends=null;
	        while ((line = br.readLine())!=null){
	            if(intends==null){
	            	intends=ForAnligla.getIntends(line);
	            }
	            
	            if(intends==null){
	            	continue;
	            }
	            
	            if(!intends.equals("null")){
	            	map=getIntend(line.trim());
	            	rowNum++;
	            	ForAnligla.fillRow(sheet,map,rowNum,line,intends);
	            	System.out.println("......***success end "+line);
	            }else{
	            	System.out.println("......skip end "+line);
	            }
	            
	            String newInt=ForAnligla.getIntends(line);
	            if(newInt!=null){
	            	intends=newInt;
	            }
	            
	        }
	        
	        
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static final String url = "http://47.74.1.189:8089/nlg/dialog?txt=";
	public static Map<String,Object> getIntend(String name){
		Map<String,Object> map=new HashMap<String, Object>();
		
		long start=System.currentTimeMillis();
		try {
			String param=URLEncoder.encode(name+"有没有");
			String obj=WebConn.doGet(url+param);
			Map pmap=JsonUtil.convertJson2Object(obj, Map.class);
			map = (Map<String,Object>)pmap.get("entry");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("******error:"+name);
		}
		long end=System.currentTimeMillis();
		
		map.put("time", end-start);
		
		return map;
	}
}
